{header}
<script type="text/javascript">
  var file_list = [];
  var remove_file_list = [];
  var is_upload_success_list = [];
</script>
<link rel="stylesheet" type="text/css" href="/static/plugins/webuploader-0.1.5/webuploader.css">
<script type="text/javascript" src="/static/plugins/webuploader-0.1.5/webuploader.min.js"></script>
<form>
  <div class="form-group">
    <label>标题</label>
    <input class="form-control" type="text" value="{title}" readonly>
  </div>
  <div class="form-group">
    <label>文案要求</label>
    <textarea class="form-control" readonly>{requirement}</textarea>
  </div>
  <div class="form-group">
    <label>选题</label>
    <input class="form-control" type="text" value="{movie_name}" readonly>
  </div>
  <div class="form-group">
    <label>领域</label>
    <input class="form-control" type="text" value="{topic_type}" readonly>
  </div>
  <div class="form-group">
    <label>Word</label>
    {down_div}
  </div>
  <div id="uploader" class="wu-example">
    <label>配音</label>
    <!--用来存放文件信息-->
    <div id="thelist" class="uploader-list"></div>
    <div class="btns">
        <div id="picker">选择文件</div>
    </div>
  </div>
  <ul class="list-group" id="upload_list">
  </ul>
  <div class="form-group">
    <label>操作</label>
    <div>
      <button class="btn btn-success" type="button" id="save">通过</button>
      <button class="btn btn-danger" type="button" id="_refuse" onclick="refuse()">拒绝</button>
    </div>
  </div>
</form>
<script type="text/javascript">
  function refuse() {
    var refuse_button = $('#_refuse');
    refuse_button.attr('disabled', true);
    $.ajax({
      url: '{base_url}library/refuse_copywrite_order',
      type: 'post',
      data: {
        id: {id}
      },
      success: function (data) {
        refuse_button.attr('disabled', false);
        var d = data.split(':');
        if (d.length !== 2) {
          console.error(data);
          layer.alert('服务器返回非法数据', {icon: 2});
          return;
        }
        if (d[0] === 'fail') {
          layer.alert('审核出错:' + d[1], {icon: 2});
          console.error('审核出错:' + d[1]);
        } else if (d[0] === 'success') {
          location.href = '{base_url}wait_for_examine?p=1';
        } else {
          console.error(data);
          layer.alert('服务器返回多余数据', {icon: 2});
        }
      }
    });
  }
  var uploader = WebUploader.create({
      // swf文件路径
      swf: '{base_url}plugins/webuploader-0.1.5/Uploader.swf',
      // 文件接收服务端。
      server: '{base_url}library/upload_dub_file',
      // 选择文件的按钮。可选。
      // 内部根据当前运行是创建，可能是input元素，也可能是flash.
      pick: '#picker',
      // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
      resize: false,
      fileNumLimit: 1
  });
  function remove_file(file_id, file_name) {
    if (file_id !== 0) {
      uploader.removeFile(file_id, true);
    }
    $('#upload_list #' + file_name).remove();
  }
  uploader.on('fileQueued', function (file) {
    var ul = $('#upload_list');
    var name = file.name.replace(/[\.#!@$%\^\&\(\)]/, '_');
    var li = $('<li class="list-group-item d-flex justify-content-between align-items-center" id="' + name + '">' + file.name + '<span class="badge badge-primary" id="' + file.id + '">待上传</span><a class="badge badge-danger" href="javascript:void(0);" onclick="remove_file(\'' + file.id + '\', \'' + name + '\')">删除</a></li>');
    ul.append(li);
  });
  uploader.on('uploadProgress', function (file, progress) {
    var li = $('#' + file.id);
    li.html(parseInt(progress * 100) + '%');
  });
  uploader.on('uploadError', function (file, reason) {
    is_upload_success_list[file.id] = false;
    console.error(reason);
    var file_id = $('#' + file.id);
    file_id.removeClass('badge-primary');
    file_id.addClass('badge-danger');
    file_id.html('上传失败');
  });
  uploader.on('uploadSuccess', function (file, response) {
    var data = response._raw.split(':');
    if (data.length !== 2) {
      is_upload_success_list[file.id] = false;
      layer.alert('服务器返回非法数据', {icon: 2});
      return;
    }
    var file_id = $('#' + file.id);
    if (data[0] === 'fail') {
      is_upload_success_list[file.id] = false;
      file_id.removeClass('badge-primary');
      file_id.addClass('badge-danger');
      file_id.html('上传失败');
      console.error(file.id + '上传失败:' + data[1]);
    } else if (data[0] === 'success') {
      is_upload_success_list[file.id] = true;
      file_list.push(data[1]);
      file_id.html('上传成功');
    } else {
      is_upload_success_list[file.id] = false;
      layer.alert('服务器返回多余数据', {icon: 2});
    }
  });
  uploader.on('uploadFinished', function () {
    for (var is_all_success in is_upload_success_list) {
      if (!is_all_success) {
        layer.alert('有文件上传失败，无法提交记录', {icon: 2});
        return;
      }
    }
    var save = $('#save');
    save.attr('disabled', true);
    $.ajax({
      url: '{base_url}library/accept_copywrite_order',
      type: 'post',
      data: {
        id: {id},
        topic_type: '{topic_type}',
        file_list: file_list.join('|')
      },
      success: function (data) {
        var d = data.split(':');
        save.attr('disabled', false);
        if (d.length !== 2) {
          console.error(data);
          layer.alert('服务器返回非法数据', {icon: 2});
          return;
        }
        if (d[0] === 'fail') {
          layer.alert('上传失败:' + d[1], {icon: 2});
          console.error('上传失败:' + d[1]);
        } else if (d[0] === 'success') {
          if (ws !== null && ws.readyState === 1) {
            ws.send('receive_id"="0";"msg"="msg"-"clip_list');
          }
          location.href = '{base_url}wait_for_examine?p=1';
        } else {
          console.error(data);
          layer.alert('服务器返回多余数据', {icon: 2});
        }
      },
      error: function (data) {
        layer.alert('上传失败', {icon: 2});
        console.error(data.responseText);
      }
    });
  });
  $(function () {
    $('#save').on('click', function () {
      uploader.upload();
    });
  });
</script>
{footer}

